package com.zsguan.z.zhttp4andr;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import com.zsguan.s.stools4andr.security.MD5;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Locale;
import java.util.UUID;

/**
 * Created by chshqiang on 15-9-12.
 * Utility
 */
public class Utility {
    private static final String DEFAULT_CHARSET = "UTF-8";

    public Utility() {
    }

    public static Bundle parseUrl(String url) {
        try {
            URL e = new URL(url);
            Bundle b = decodeUrl(e.getQuery());
            b.putAll(decodeUrl(e.getRef()));
            return b;
        } catch (MalformedURLException var3) {
            return new Bundle();
        }
    }

    public static Bundle parseUri(String uri) {
        try {
            URI e = new URI(uri);
            Bundle b = decodeUrl(e.getQuery());
            return b;
        } catch (Exception var3) {
            return new Bundle();
        }
    }

    public static Bundle decodeUrl(String s) {
        Bundle params = new Bundle();
        if(s != null) {
            String[] array = s.split("&");

            for (String parameter : array) {
                String[] v = parameter.split("=");

                try {
                    params.putString(URLDecoder.decode(v[0], "UTF-8"), URLDecoder.decode(v[1], "UTF-8"));
                } catch (UnsupportedEncodingException var9) {
                    var9.printStackTrace();
                }
            }
        }

        return params;
    }

    public static boolean isChineseLocale(Context context) {
        try {
            Locale e = context.getResources().getConfiguration().locale;
            return Locale.CHINA.equals(e) || Locale.CHINESE.equals(e) || Locale.SIMPLIFIED_CHINESE.equals(e) || Locale.TAIWAN.equals(e);
        } catch (Exception var2) {
            return true;
        }
    }

    public static String generateGUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String getSign(Context context, String pkgName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 64);
        } catch (PackageManager.NameNotFoundException var5) {
            return null;
        }

        //noinspection LoopStatementThatDoesntLoop
        for(int j = 0; j < packageInfo.signatures.length; ++j) {
            byte[] str = packageInfo.signatures[j].toByteArray();
            return MD5.hexdigest(str);
        }

        return null;
    }

    public static String safeString(String orignal) {
        return TextUtils.isEmpty(orignal)?"":orignal;
    }

    public static String getAid(Context context, String appKey) {
//        AidTask task = AidTask.getInstance(context);
//        AidInfo aidInfo = task.getAidSync(appKey);
//        return aidInfo != null?aidInfo.getAid():"";
        return "";
    }

    public static String generateUA() {
        return Build.MANUFACTURER + "-" + Build.MODEL + "_" + Build.VERSION.RELEASE + "_" + "weibosdk" + "_" + "0031005000" + "_android";
    }

}
